/*
 * jQuery File Upload Plugin JS Example 8.9.1
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */

/* global $, window */

$(function () {
    'use strict';
    
    var url_gallery = $('#gallery_upload').attr('action');
    // Initialize the jQuery File Upload widget:
    $('#gallery_upload').fileupload({
        url: url_gallery,
        disableImageResize: false,
        autoUpload: false,
        maxFileSize: 20000000,
        multipart: true,
        loadImageMaxFileSize: 20000000,
    });
    
    $('#gallery_upload').bind('fileuploadsubmit', function (e, data) {
        var inputs      = data.context.find(':input');
        data.formData   = inputs.serializeArray();
        
        console.log(data.formData);
        return false;
    });
    
    $('#gallery_upload').bind('fileuploadadded', function (e, data) {
        $('.image_date').datetimepicker({
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
        });
    });
    
    // Enable iframe cross-domain access via redirect option:
    $('#gallery_upload').fileupload(
        'option',
        'redirect',
        window.location.href.replace(
            /\/[^\/]*$/,
            '/cors/result.html?%s'
        )
    );
});
